<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use app\model\Bazaar;
use app\model\Expand;
use app\model\Campaign;
use app\model\Customer;
use app\model\Principal;

class SemCopy extends Common
{

	public function method(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'semcopy:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
		array_push( $where, ['userType', '=', 1 ] );

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if($bazaarId) {
			array_push( $where, ['bazaarId', '=',  $bazaarId ] );
		}

		$userid = $request->param('userid', '', 'trim');
		if($userid) {
			array_push( $where, ['userid', '=',  $userid ] );
		}
		
		$customerid = $request->param('customerid', '', 'trim');
		if($customerid) {
			array_push( $where, ['customerid', '=',  $customerid ] );
		}

		$contact = $request->param('contact', '', 'trim');
		if($contact) {
			array_push( $where, ['contact', '=',  $contact ] );
		}

 		$campaignId = $request->param('campaignId', '', 'trim');
		if($campaignId) {
			array_push( $where, ['campaignId', 'in',  $campaignId ] );
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
		} else {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true) ] );
		}
        $roleTempId = Db::table('cfg_user')->where('id', $request->sessInfo['id'])->value('role');
		if($roleTempId) {
			$authaccount = Db::table('cfg_role')->where('companyId', $request->sessInfo['companyId'])->where('id', $roleTempId)->value('semauthaccount');
			if($authaccount) {
				array_push( $where, ['userid', 'in',  $authaccount ] );
			}
		}
		$query = Db::name('cfg_search')->field('`username`, `bazaarId`, `campaign`, `adgroup`, `keyword`, sum(`impression`) as `impression`, sum(`clicks`) as `clicks`, ROUND(sum(`cost`) / sum(`clicks`), 2) as `cpc`, ROUND(sum(`clicks`) / sum(`impression`) * 100, 2) as `ctr`,
		  sum(`cost`) as `cost`, sum(`cash`) as `cash`, sum(`copynumber`) as `copynumber`,
			IF(ROUND( sum(`cost`) / sum(`copynumber`), 2 ), ROUND( sum(`cost`) / sum(`copynumber`), 2 ), sum(`cost`)) as `copycost`')
				->group('keywordid')->where($where)->order($sort, $order)->paginate($limit);

		$total = Db::name('cfg_search')->field('sum(`impression`) as `impression`, sum(`clicks`) as `clicks`, ROUND(sum(`cost`) / sum(`clicks`), 2) as `cpc`, ROUND(sum(`clicks`) / sum(`impression`) * 100, 2) as `ctr`,
		  sum(`cost`) as `cost`, sum(`cash`) as `cash`, sum(`copynumber`) as `copynumber`, IF(ROUND( sum(`cost`) / sum(`copynumber`), 2 ), ROUND( sum(`cost`) / sum(`copynumber`), 2 ), sum(`cost`)) as `copycost`')->where($where)->find();

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items(), 'total' => $total,
			'expand' => Expand::getList($request->sessInfo, 1),
		  'customer' => Customer::getList($request->sessInfo),
		  'principal' => Principal::getList($request->sessInfo),
			'bazaar' => Bazaar::getList($request->sessInfo, 1) ];
	}

	public function getWord(Request $request) {
		$username = $request->param('username', '', 'trim');
		if( empty($username)) {
			return ['code'=> 0, 'semanalyse'=> [] ];
		}

		$keyword = $request->param('keyword', '', 'trim');
		if( empty($keyword)) {
			return ['code'=> 0, 'semanalyse'=> [] ];
		}

		$where = [];
		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
		array_push( $where, ['userType', '=', 1 ] );

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
		} else {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), true) ] );
		}

		$keyword = $request->param('keyword', '', 'trim');
		if($keyword) {
			array_push( $where, ['keyword', '=',  $keyword ] );
		}

		$semanalyse = Db::name('cfg_search')->field('username, campaign, word, impression, clicks, cost, cash, cpc, ctr, copynumber, IF(ROUND( `cost` / `copynumber`, 2 ), ROUND( `cost` / `copynumber`, 2 ), `cost`) as copycost')->where($where)
		  ->order('copycost', 'desc')->select()->toArray();
		return ['code'=> 0, 'semanalyse' => $semanalyse];
	}

	public function getCampaign(Request $request) {
		$userid = $request->param('userid', '', 'trim');
		if( empty($userid)) {
			return ['code'=> 0, 'campaign'=> [] ];
		}
		return ['code'=> 0, 'campaign'=> Campaign::getList($request->sessInfo, $userid)];
	}

}
